<?php

namespace common\observer;

use support\SingletonTrait;
use common\model\VersionModel;
use Webman\Stomp\Client;
use teamones\dbeventObserver\interfaces\SplSubject;
use teamones\dbeventObserver\interfaces\SplObserver;

/**
 * 内部任务完成 查看最后一个已发布版本是否需要发送给乙方
 * @update 2022年5月9日 暂时转移到版本的发布事件
 */
class InsideTaskDoneVersionOrderTaskUpstreamCheck implements SplObserver
{
    use SingletonTrait;

    /**
     * @inheritDoc
     */
    function update(SplSubject $subject): void
    {
        $updateData = $subject->getSplUpdateData();
        if (!isset($updateData['task_status'])) {
            return;
        }
        $task = $subject->getSubjectData();
        // 如果是内部任务完成 那么需要检测需要传递给乙方
        $isInsideExecute = $task['executor_tenant_id'] == 0 || $task['tenant_id'] == $task['executor_tenant_id'];
        if (!$isInsideExecute) {
            return;
        }
        $latestVersionFilter = [
            'module_id' => module_by_code('task')['id'],
            'link_id' => $task['id'],
            'is_pass' => 'yes',
            'is_publish' => 'yes',
        ];
        $latestVersion = model(VersionModel::class)->where($latestVersionFilter)->order('id desc')->find();
        if (empty($latestVersion)) {
            return;
        }
        Client::send('version_order_task_upstream_check', build_queue_data_with_xu(['version_id' => $latestVersion['id']]), 3);
    }
}
